package org.linlinjava.litemall.wx.web;

import static org.linlinjava.litemall.wx.util.WxResponseCode.COMMON_DETAIL_IS_NULL;

import java.time.LocalDateTime;
import java.util.List;

import javax.validation.constraints.NotNull;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.linlinjava.litemall.core.util.ResponseUtil;
import org.linlinjava.litemall.core.validator.Order;
import org.linlinjava.litemall.core.validator.Sort;
import org.linlinjava.litemall.db.domain.LitemallQuotePrice;
import org.linlinjava.litemall.db.service.LitemallQuotePriceService;
import org.linlinjava.litemall.wx.annotation.LoginUser;
import org.linlinjava.litemall.wx.service.WxQualificationsService;
import org.linlinjava.litemall.wx.service.WxQuoteService;
import org.linlinjava.litemall.wx.vo.QualificationsVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * 药材报价
 */
@RestController
@RequestMapping("/wx/quote")
@Validated
public class WxQuoteController {
	private final Log logger = LogFactory.getLog(WxQuoteController.class);

	@Autowired
	private WxQualificationsService wxQualificationsService;

	@Autowired
	private WxQuoteService wxQuoteService;
	
	@Autowired
	private LitemallQuotePriceService litemallQuotePriceService;

	@GetMapping("list")
	public Object list(@LoginUser Integer userId, @NotNull Byte type, @RequestParam(defaultValue = "1") Integer page,
			@RequestParam(defaultValue = "10") Integer limit,
			@Sort @RequestParam(defaultValue = "add_time") String sort,
			@Order @RequestParam(defaultValue = "desc") String order,
			@RequestParam(defaultValue = "") String searchValue) {

		return wxQuoteService.list(userId, type, page, limit, sort, order, searchValue);
	}

	@GetMapping("detail")
	public Object detail(@LoginUser Integer userId, @NotNull Integer id, @NotNull Integer itemId) {
		if (userId == null) {
			return ResponseUtil.unlogin();
		}
		return wxQuoteService.detail(userId, id, itemId);
	}

	@PostMapping("save")
	public Object save(@LoginUser Integer userId, @RequestBody List<LitemallQuotePrice> quotePriceList) {
		if (userId == null) {
			return ResponseUtil.unlogin();
		}
		

		QualificationsVo qualificationsVo = wxQualificationsService.getQualificationsVo(userId);
		if (qualificationsVo == null) {
			return ResponseUtil.fail(COMMON_DETAIL_IS_NULL, "未获取到资质信息，请刷新重试");
		}
		
		for(LitemallQuotePrice quotePrice : quotePriceList) {
	        quotePrice.setSupplyShopId(qualificationsVo.getId());
	        quotePrice.setSupplyUserType(qualificationsVo.getUserType());
	        quotePrice.setSupplyContactName(qualificationsVo.getContactName());
	        quotePrice.setSupplyContactTel(qualificationsVo.getContactTel());
	        
	        quotePrice.setId(null);
	        quotePrice.setAddTime(LocalDateTime.now());
	        quotePrice.setUpdateTime(LocalDateTime.now());
	        quotePrice.setUserId(userId);
	        quotePrice.setStatus((byte)0);
	        quotePrice.setDeleted(false);
		}
		
		litemallQuotePriceService.addAll(quotePriceList);

		return ResponseUtil.ok();
	}
}